﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Eis51.CampanyCMS.Model.BaseInfoMng;
using Eis51.CampanyCMS.Business.BaseInfoMng;
using System.IO;

namespace Eis51.CampanyCMS.WebUI.Admin.TemplatesMgnt
{
    public partial class edit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitData();

            }
        }

        private void InitData()
        {
            if (Request.QueryString["templateid"] != null)
            {
                BaseTemplates obj = BaseTemplatesBLL.GetInstance().Get(new BaseTemplates.Key(int.Parse(Request.QueryString["templateid"].ToString())));
                txtDesc.Text = obj.Desc;
                txtTemplateFolder.Text = obj.TemplateFloder;
                txtTemplateName.Text = obj.TemplateName;
                txtLimitLevel.Text = obj.LimitNum.ToString();
                lblImg.Text = "<img src='" + Page.ResolveUrl(obj.TemplateThumbnailPath) + "' />";

            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            BaseTemplates obj = new BaseTemplates();
            if (Request.QueryString["templateid"] != null)
            {
                obj = BaseTemplatesBLL.GetInstance().Get(new BaseTemplates.Key(int.Parse(Request.QueryString["templateid"].ToString())));
            }
            else
            {
                obj = new BaseTemplates();
            }
            //上传图片
            string bigImg = FileUpload1.PostedFile.FileName;
            string folderPath = Server.MapPath("~/UploadFile/");
            if (bigImg != string.Empty)
            {
                string path = Path.Combine(folderPath, "TemplateImg");
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                FileInfo fi = new FileInfo(FileUpload1.PostedFile.FileName);
                obj.TemplateThumbnailPath = Path.Combine("~/UploadFile/TemplateImg", DateTime.Now.ToString("yyyyMMddHHmmss") + fi.Name);
                string uploadPath = Server.MapPath(obj.TemplateThumbnailPath);
                this.FileUpload1.PostedFile.SaveAs(uploadPath);
            }
            obj.Desc = txtDesc.Text;
            obj.TemplateFloder = txtTemplateFolder.Text;
            obj.TemplateName = txtTemplateName.Text;
            obj.LimitNum = int.Parse(txtLimitLevel.Text);

            BaseTemplatesBLL.GetInstance().Submit(obj);
            btnReturn_Click(null, null);
        }

        protected void btnReturn_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["r"] != null)
            {
                Response.Redirect(Request.QueryString["r"]);
            }
        }
    }
}
